using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_AllAcount : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCurent();
        LoadData();
    }
    private void LoadCurent()
    {
        HtmlGenericControl ulAdmin = (HtmlGenericControl)Master.FindControl("ulAdmin");
        ulAdmin.Attributes.Add("class", "current");
        HtmlGenericControl liSubShow = (HtmlGenericControl)Master.FindControl("liSubShow");
        liSubShow.Attributes.Add("class", "sub_show");
        HtmlControl divSelectSub = (HtmlControl)Master.FindControl("divSelectSub");
        divSelectSub.Attributes.Add("class", "select_sub show");
    }
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {
        Criteria cri = new Criteria();
        //hyperlinkPager.PageSize = RTISettings.PageSize;
        //NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
       // int totalrecord = 0;
        //UserCollection _userCollection = UserService.GetUserListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);
        UserCollection _userCollection = UserService.GetUserListByParam(UserColumns.UserId, OrderDirection.ASC, cri.Criter);
        rptList.DataSource = _userCollection;
        rptList.DataBind();
        ltrNum.Text = _userCollection.Count.ToString();
        //hyperlinkPager.ItemCount = totalrecord;
        //if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
        //    hyperlinkPager.Visible = false;
    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            User _user = (User)e.Item.DataItem;

            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            hidID.Value = _user.UserId.ToString();
            int STT = NoCountStart + e.Item.ItemIndex;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";

            }
        }
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox ckbDelete = (CheckBox)item.FindControl("ckbDelete");
            if (ckbDelete.Checked)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                Criteria criuserPermission = new Criteria();
                criuserPermission.add("[" + ADESSA.Entities.User._Table + "].[" + UserColumns.UserId + "]", long.Parse(strhidID), Condition.EQUAL, Condition.AND);
                UserCollection _PermissionCollection = UserService.GetUserListByParam("[" + ADESSA.Entities.User._Table + "].[" + UserColumns.UserId + "]", OrderDirection.ASC, criuserPermission.Criter);
                ADESSA.Entities.User _User = new User();
                if (_PermissionCollection.Count > 0)
                {
                    _User = _PermissionCollection[0];
                }
                if (Session["username"] != null)
                {
                    if (Session["username"].ToString().ToUpper() == FetConstant.Root.ToUpper())
                    {
                        UserService.DeleteUser(ID);
                       // lblerror.Text = string.Empty;
                        Response.Redirect(Request.Url.ToString());
                    }
                }
                long user = long.MinValue;
                if (Session["checked"] != null)
                {
                    user = long.Parse(Session["checked"].ToString());
                    if (_User.PermissionId == user)
                    {
                       // lblerror.Text = Utilitys.Dictionary["DeleteErrorCreateUser"].ToString();
                        return;
                    }
                }
                try
                {
                    UserService.DeleteUser(ID);
                   // lblerror.Text = string.Empty;
                    Response.Redirect(Request.Url.ToString());
                }
                catch
                {
                   // lblerror.Text = Utilitys.Dictionary["DeleteErrorCreateUser"].ToString();
                }
            }
        }
        LoadData();
    }
    #endregion
}
